
(function(){
    /*
     * 用于记录日期，显示的时候，根据dateObj中的日期的年月显示
     */
    var dateObj = (function(){
        var _date = new Date();    // 默认为当前系统时间
        return {
            getDate : function(){
                return _date;
            },
            setDate : function(date) {
                _date = date;
            }
        };
    })();

    // 设置calendar div中的html部分
    renderHtml();
    // 表格中显示日期
    showCalendarData();
    // 绑定事件
    bindEvent();
    function renderHtml() {
        var calendar = document.getElementById("calendar");
        var headeBox = document.getElementById("headerBox");
        var headeBoxE = document.getElementById("headerBoxE");
        var bodyBox = document.getElementById("bodyBox");
        var _headHtml =
            "<tr>" +
            "<th>日</th>" +
            "<th>一</th>" +
            "<th>二</th>" +
            "<th>三</th>" +
            "<th>四</th>" +
            "<th>五</th>" +
            "<th>六</th>" +
            "</tr>";
        var _bodyHtml = "";
        for(var i = 0; i < 6; i++) {
            _bodyHtml += "<tr>" +
            "<td></td>" +
            "<td></td>" +
            "<td></td>" +
            "<td></td>" +
            "<td></td>" +
            "<td></td>" +
            "<td></td>" +
            "</tr>";
        }
        headerBox.innerHTML="<table id='calendarHTable' class='calendar-Htable'>" +
        _headHtml + "</table>";
        bodyBox.innerHTML = "<div class='container'><table id='calendarTable' class='calendar-table'>" +
        _bodyHtml +
        "</table></div>";
        // 添加到calendar div中
        calendar.appendChild(headerBox);
        calendar.appendChild(bodyBox);
    }
    function showCalendarData(){
        var _year = dateObj.getDate().getFullYear();
        var _month = dateObj.getDate().getMonth() + 1;
        var _dateStr = getDateStr(dateObj.getDate());
        // 设置顶部标题栏中的 年、月信息
        var calendarTitleY = document.getElementById("calendarTitleY");
        var titleStr = _dateStr.substr(0, 4) + "年";
        var titleStr1 = _dateStr.substr(4,2) + "月";
        calendarTitleY.innerText = titleStr;
        calendarTitleM.innerText = titleStr1;
        // 设置表格中的日期数据
        var _table = document.getElementById("calendarTable");
        var _tds = _table.getElementsByTagName("td");
        var _firstDay = new Date(_year, _month - 1, 1);  // 当前月第一天
        for(var i = 0; i < _tds.length; i++) {
            var _thisDay = new Date(_year, _month - 1, i + 1 - _firstDay.getDay());//当月第一天星期几减去几
            var _thisDayStr = getDateStr(_thisDay);
            _tds[i].innerText = _thisDay.getDate();
            //_tds[i].data = _thisDayStr;
            _tds[i].setAttribute('data', _thisDayStr);
            if(_thisDayStr == getDateStr(new Date())) {    // 当前天
                _tds[i].className = 'currentDay';
            }else if(_thisDayStr.substr(0, 6) == getDateStr(_firstDay).substr(0, 6)) {
                _tds[i].className = 'currentMonth';  // 当前月
            }else {    // 其他月
                _tds[i].className = 'otherMonth';
            }
        }
    }
    /**
     * 绑定上个月下个月事件
     */
    function bindEvent() {
        var prevMonth = document.getElementById("prevMonth");
        var nextMonth = document.getElementById("nextMonth");
        var prevYear = document.getElementById("prevYear");
        var nextYear  = document.getElementById("nextYear");
        addEvent(prevMonth, 'click', toPrevMonth);
        addEvent(nextMonth, 'click', toNextMonth);
        addEvent(prevYear, 'click', toPrevYear);
        addEvent(nextYear, 'click', toNextYear);
    }
    /**
     * 绑定事件
     */

    function addEvent(dom, eType, func) {
        if(dom.addEventListener) {  // DOM 2.0
            dom.addEventListener(eType, function(e){
                func(e);
            });
        } else if(dom.attachEvent){  // IE5+
            dom.attachEvent('on' + eType, function(e){
                func(e);
            });
        } else {  // DOM 0
            dom['on' + eType] = function(e) {
                func(e);
            }
        }
    }
    /**
     * 点击上个月图标触发
     */
    $("#calendarTable").swipeRight (function(){
        toPrevMonth()
    });
    $("#calendarTable").swipeLeft (function(){
        toNextMonth()
    });
    function toPrevMonth() {
        var date = dateObj.getDate();
        dateObj.setDate(new Date(date.getFullYear(), date.getMonth() - 1, 1));
        $(".container table").css({"transform":"translateX(100%)","transition":"all 1s"});
        setTimeout(function(){
            $(".container table").css({"transform":"translateX(0)","transition":"all 0s"})
        },1000);
        showCalendarData();
    }
    /**
     * 点击下个月图标触发
     */
    function toNextMonth() {
        var date = dateObj.getDate();
        dateObj.setDate(new Date(date.getFullYear(), date.getMonth() + 1, 1));
        $(".container table").css({"transform":"translateX(-100%)","transition":"all 1s"});
        setTimeout(function(){
            $(".container table").css({"transform":"translateX(0)","transition":"all 0s"})
        },1000);
        showCalendarData();
    }
    function toPrevYear() {
        var date = dateObj.getDate();
        dateObj.setDate(new Date(date.getFullYear()- 1, date.getMonth() , 1));
        $(".container table").css({"transform":"translateX(100%)","transition":"all 1s"});
        setTimeout(function(){
            $(".container table").css({"transform":"translateX(0)","transition":"all 0s"})
        },1000);
        showCalendarData();
    }
    /**
     * 点击下个月图标触发
     */
    function toNextYear() {
        var date = dateObj.getDate();
        dateObj.setDate(new Date(date.getFullYear()+ 1, date.getMonth() , 1));
        $(".container table").css({"transform":"translateX(-100%)","transition":"all 1s"});
        setTimeout(function(){
            $(".container table").css({"transform":"translateX(0)","transition":"all 0s"})
        },1000);
        showCalendarData();
    }
    /**
     * 日期转化为字符串， 4位年+2位月+2位日
     */
    function getDateStr(date) {
        var _year = date.getFullYear();
        var _month = date.getMonth() + 1;    // 月从0开始计数
        var _d = date.getDate();
        _month = (_month > 9) ? ("" + _month) : ("0" + _month);
        _d = (_d > 9) ? ("" + _d) : ("0" + _d);
        return _year + _month + _d;
    }
})();
